package net.cellkyborg.server;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class UpdateServer extends Thread {
	
	private Socket sock = null;
	private InputStream in = null;
	private OutputStream out = null;
	private stream inStream, outStream = null;
	
	public UpdateServer(Socket s) {
		sock = s;
	}
	
	public void run() {
		try {
			in = sock.getInputStream();
			out = sock.getOutputStream();
			
			outStream = new stream(new byte[1000000]);
			outStream.writeOffset = 0;
			outStream.readOffset = 0;
			inStream = new stream(new byte[1000000]);
			inStream.writeOffset = 0;
			inStream.readOffset = 0;

			fillInStream(5);
			
			outStream.writeByte(0);
			directFlushOutStream();

			fillInStream(8);
		
			for (int i=0;i<136;i++) {
				outStream.writeByte(server.updateHandler.UKEYS[i]);
			}
	
			directFlushOutStream();

			fillInStream(4);

			sock.close();
		} catch (IOException ioex) { }
	}
	
	private void fillInStream(int forceRead) throws IOException {
		int rd = in.read(inStream.buffer, inStream.writeOffset, forceRead);

		inStream.writeOffset+=rd;			
	}
	
	private void directFlushOutStream() throws IOException {
		out.write(outStream.buffer, 0, outStream.writeOffset);
		outStream.writeOffset = 0;		// reset
	}
}
